package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.LoadDailyReportFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.PeopleInformationFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.entity.ZInstallcheackFromMap;
import com.iemes.mapper.mould.LoadDailyReportMapper;
import com.iemes.mapper.mould.MouldUnpassHandleMapper;
import com.iemes.mapper.mould.ZInstallcheackMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/zInstall/")
@Controller
public class ZInstallcheakController extends BaseController {

	@Inject
	private ZInstallcheackMapper zInstallcheackMapper;
	
	@Inject
	private MouldUnpassHandleMapper mouldUnpassHandleMapper;
	
	@Inject
	private LoadDailyReportMapper loadDailyReportMapper;
	
	@RequestMapping("examineOrder")
	public String examineOrder(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/zInstallcheck/zInstallcheck_list";
	}
	
	@ResponseBody
	@RequestMapping("zInstall_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		ZInstallcheackFromMap zInstallcheackFromMap = getFormMap(ZInstallcheackFromMap.class);
		List<Map<String, Object>> zinstall = zInstallcheackMapper.getAll(zInstallcheackFromMap);
		LuogicPaging(pageNow, pageSize, zinstall);
		return pageView;
	}
	
	@RequestMapping("zInstall_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		List<PeopleInformationFromMap> people=baseMapper.findByWhere(new PeopleInformationFromMap());
		model.addAttribute("people", people);
		//以后要改为用PeopleInformationFromMap实体查找维修员
		List<UserFormMap> users=baseMapper.findByWhere(new UserFormMap());
		model.addAttribute("users",users);
		if(Common.isNotEmpty(id)){
			//根据id查询装模质量检查表
			ZInstallcheackFromMap install = zInstallcheackMapper.getById(id);
			model.addAttribute("install",install);
			//获得相同checkId的检查标准
			MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
			mouldUnpassHandleFormMap.put("checkId", install.get("id"));
			List<MouldUnpassHandleFormMap> handle=mouldUnpassHandleMapper.getzInstallHandle(mouldUnpassHandleFormMap);
			model.addAttribute("handle",handle);
		}
		return Common.BACKGROUND_PATH + "/mould/zInstallcheck/zInstallcheck_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="zInstall_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="装模质量检查",methods="装模质量检查表-编辑装模质量检查表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		ZInstallcheackFromMap zInstallcheackFromMap=getFormMap(ZInstallcheackFromMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		zInstallcheackFromMap.put("checkDate", sdf.format(date));
		zInstallcheackFromMap.put("checkOnlyDate", sdf2.format(date));
		zInstallcheackFromMap.put("checkPeopleId", userFormMap.getInt("id"));
		zInstallcheackFromMap.put("adjustPeopleId", userFormMap.getInt("id"));
		zInstallcheackFromMap.put("status", 1);
		baseMapper.editEntity(zInstallcheackFromMap);
		//添加上一次与本次相同装模模具的装模日期
		String mold_encode=request.getParameter("zInstallcheackFromMap.mold_encode");
		ZInstallcheackFromMap getByMaxDate=zInstallcheackMapper.getByMaxDate(mold_encode);
		if(getByMaxDate!=null){
			String lastTimeLoad=getByMaxDate.getStr("checkDate");
			zInstallcheackFromMap.put("lastTimeLoad", lastTimeLoad);
			baseMapper.editEntity(zInstallcheackFromMap);
		}
		//先删除
		baseMapper.deleteByAttribute("checkId","'"+Integer.parseInt(zInstallcheackFromMap.getStr("id"))+"'", MouldUnpassHandleFormMap.class);
		//再添加
		String checkId=request.getParameter("zInstallcheackFromMap.id");
		String[] standardId=request.getParameterValues("standardId_1");
		String[] checkResult=request.getParameterValues("checkResult_1");
		String[] handle=request.getParameterValues("handle_1");
		String[] handleTime=request.getParameterValues("handleTime_1");
		String[] repairPeopleId=request.getParameterValues("repairPeopleId_1");
		String[] repairResult=request.getParameterValues("repairResult_1");
		String[] remark=request.getParameterValues("remark_1");
		String[] finishRepair=request.getParameterValues("finishRepair_1");
		String[] finishTime=request.getParameterValues("finishTime_1");
		if(standardId!=null){
			for(int i=0;i<standardId.length;i++){
				MouldUnpassHandleFormMap mouldUnpassHandleFormMap=new MouldUnpassHandleFormMap();
				mouldUnpassHandleFormMap.put("checkId", checkId);
				mouldUnpassHandleFormMap.put("standardId", standardId[i]);
				mouldUnpassHandleFormMap.put("checkResult", checkResult[i]);
				mouldUnpassHandleFormMap.put("handle",handle[i]);
				mouldUnpassHandleFormMap.put("handleTime", handleTime[i]);
				mouldUnpassHandleFormMap.put("repairPeopleId", Integer.parseInt(repairPeopleId[i]));
				mouldUnpassHandleFormMap.put("repairResult",repairResult[i]);
				mouldUnpassHandleFormMap.put("remark",remark[i]);
				mouldUnpassHandleFormMap.put("finishRepair", Integer.parseInt(finishRepair[i]));
				mouldUnpassHandleFormMap.put("finishTime",finishTime[i]);
				mouldUnpassHandleFormMap.put("supervisorId", userFormMap.getInt("id"));
				baseMapper.addEntity(mouldUnpassHandleFormMap);
			}
		}
		//添加装模日报表
		String installCheckId=zInstallcheackFromMap.get("id").toString();
		ZInstallcheackFromMap install=zInstallcheackMapper.getById2(installCheckId);
		//获取够产时间
		String enoughTime=install.getStr("enoughTime");
		//获取开始拆模时间
		String uBeginTime=install.getStr("uBeginTime");
		//获取开始检查时间
		String beginCheck=install.getStr("beginCheck");
		//获取叉模完成时间
		String endTime=install.getStr("endTime");
		//获取检查完成时间
		String endCheck=install.getStr("endCheck");
		//获取开始生产时间？？？
		
		LoadDailyReportFormMap loadDailyReportFormMap=new LoadDailyReportFormMap();
		if(enoughTime!=null && uBeginTime!=null){
			//计算待装模时间
			loadDailyReportFormMap.put("installWaiting", MyDateUtils.intervalHour(uBeginTime, enoughTime));
		}
		if(beginCheck!=null && uBeginTime!=null){
			//计算装模完成时间
			loadDailyReportFormMap.put("finishTime",MyDateUtils.intervalHour(beginCheck, uBeginTime));
		}
		if(uBeginTime!=null && endTime!=null){
			//计算待换模时间
			loadDailyReportFormMap.put("changeWaiting",MyDateUtils.intervalHour(uBeginTime, endTime));
		}
		if(endCheck!=null && beginCheck!=null){
			//计算返工时间
			loadDailyReportFormMap.put("reworkTime",MyDateUtils.intervalHour(endCheck, beginCheck));
		}
		if(endCheck!=null && enoughTime!=null){
			//计算总耗时
			loadDailyReportFormMap.put("loadTotalTime", MyDateUtils.intervalHour(endCheck, enoughTime));
		}
		//计算调试时间
		loadDailyReportFormMap.put("adjustTime","？？？");
		LoadDailyReportFormMap loadDaily=loadDailyReportMapper.getByInstallCheckId(installCheckId);
		//获取合格判定
		if(Arrays.asList(checkResult).contains("2")){
			loadDailyReportFormMap.put("judge", 2);
		}else{
			loadDailyReportFormMap.put("judge", 1);
		}
		//获取不合格原因
		StringBuffer sbf=new StringBuffer();
		for(int i=0;i<handle.length;i++){
			if(!handle[i].isEmpty()){
				sbf.append(handle[i]+"，");
			}
		}
		if(sbf!=null && sbf.length()>0){
			loadDailyReportFormMap.put("reason", sbf.substring(0,sbf.length()-1));
		}
		if(loadDaily==null || loadDaily.size()<=0){
			loadDailyReportFormMap.put("installCheckId", zInstallcheackFromMap.get("id"));
			loadDailyReportFormMap.put("loadTime", zInstallcheackFromMap.get("checkDate"));
			baseMapper.addEntity(loadDailyReportFormMap);
		}else{
			loadDailyReportFormMap.put("id",loadDaily.get("id"));
			loadDailyReportFormMap.put("loadTime", zInstallcheackFromMap.get("checkDate"));
			baseMapper.editEntity(loadDailyReportFormMap);
		}
		return "success";
	}
}






